/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.capex.javaee.smtp;

import com.sun.mail.util.BASE64DecoderStream;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.URLName;

public class JavaAttachment {

    public static void main(String[] args) throws Exception {
        //URLName server = new URLName("protocol://username@host/foldername");
        URLName server = new URLName("pop3://evegaluna@gmail.com/INBOX");

        Properties props = new Properties();

        String hostMailServer = "pop.gmail.com";
        String hostMailPort = "995";
        props.setProperty("mail.pop3.port", hostMailPort);
        props.put("mail.pop3.host", hostMailServer);
        props.put("mail.store.protocol", "pop3");
        props.put("mail.pop3.ssl.enable", "true");

        Session session = Session.getDefaultInstance(props, new MailAuthenticator());

        Store store = session.getStore();
        store.connect(hostMailServer, 995, "evegaluna@gmail.com", "k2i8k9o7");

        Folder folder = store.getDefaultFolder().getFolder("INBOX");
        if (folder == null) {
            System.out.println("Folder " + server.getFile() + " not found.");
            System.exit(1);
        }
        folder.open(Folder.READ_ONLY);
        Message[] messages = folder.getMessages();
        for (int i = messages.length - 1; i >= 0; i--) {
            Message message = messages[i];

            if (message.getContent() instanceof Multipart) {
                Multipart multipart = (Multipart) message.getContent();
                // System.out.println(multipart.getCount());

                for (int j = 0; j < multipart.getCount(); j++) {
                    BodyPart bodyPart = multipart.getBodyPart(j);
                    if (!Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition())) {
                        continue; // dealing with attachments only
                    }
                    System.out.println(bodyPart.getFileName());
                    if (bodyPart.getContent() instanceof BASE64DecoderStream) {
                        BASE64DecoderStream contenido = (BASE64DecoderStream) bodyPart.getContent();
                        
                        String cosas = "";

                        int bit;
                        while((bit = contenido.read()) > 0){
                            cosas += ""+(char)bit;
                        }
                        System.out.println(cosas);
                    }
                }
            }

        }
        folder.close(false);
        store.close();
    }
}

//class MailAuthenticator extends Authenticator {
//
//    public MailAuthenticator() {
//    }
//
//    @Override
//    public PasswordAuthentication getPasswordAuthentication() {
//        return new PasswordAuthentication("evegaluna@gmail.com", "k2i8k9o7");
//    }
//}
